<template>
  <div class="tui-conversation" @click="handleClickConv" @touchstart="handleTouchStart" @touchend="handleTouchEnd">
    <TUISearch searchType="global" />
    <ConversationHeader v-if="isShowConversationHeader" ref="headerRef" />
    <ConversationNetwork />

    <ConversationList ref="conversationListDomRef" class="tui-conversation-list"
      @handleSwitchConversation="handleSwitchConversation" @getPassingRef="getPassingRef" />
  </div>
</template>
<script lang="ts" setup>
  import { TUILogin } from "@tencentcloud/tui-core";
  import { TUIStore, StoreName } from '@tencentcloud/chat-uikit-engine';
  import { TUIGlobal } from '@tencentcloud/universal-api';
  import { ref } from '../../adapter-vue';
  import TUISearch from '../TUISearch/index.vue';
  import ConversationList from './conversation-list/index.vue';
  import ConversationHeader from './conversation-header/index.vue';
  import ConversationNetwork from './conversation-network/index.vue';
  import { onHide, onShow } from '@dcloudio/uni-app'; // 该方法只能用在父组件内，子组件内不生效
  // import { zxbAjax } from '../index.ts';
  // zxbAjax('addons/shop/api.goods/index', res1=>{
  //       console.log('res1111---------->', res1)
  //     })
  const { chat } = TUILogin.getContext();
  onShow(()=>{
    console.log('消息界面',)
    if(!uni.getStorageSync('token')){
      uni.reLaunch({
        url:'/pages/login/login'
      })
    }else{
      // // 获取全量的会话列表
      // let promise = chat.getConversationList();
      // promise.then(function(imResponse) {
      //   // 全量的会话列表，用该列表覆盖原有的会话列表
      //   const conversationList = imResponse.data.conversationList;
      //   console.log('获取所有的绘画列表----', conversationList)
      //   // conversationListDomRef.value?.onConversationListUpdated(conversationList)
      //   // 从云端同步会话列表是否完成
      //   const isSyncCompleted = imResponse.data.isSyncCompleted;
      // }).catch(function(imError) {
      //   // 获取会话列表失败的相关信息
      //   console.warn('getConversationList error:', imError);
      // });

    }
    
  })
  const emits = defineEmits(['handleSwitchConversation']);

  const totalUnreadCount = ref(0);
  const headerRef = ref<typeof ConversationHeader>();
  const conversationListDomRef = ref<typeof ConversationList>();
  const touchX = ref<number>(0);
  const touchY = ref<number>(0);
  const isShowConversationHeader = ref<boolean>(true);

  TUIStore.watch(StoreName.CONV, {
    totalUnreadCount: (count : number) => {
      console.log('39----',count)
      totalUnreadCount.value = count;
    },
  });

  TUIStore.watch(StoreName.CUSTOM, {
    isShowConversationHeader: (showStatus : boolean) => {
      console.log('45----',showStatus)
      isShowConversationHeader.value = showStatus !== false;
    },
  });

  const handleSwitchConversation = (conversationID : string) => {
    TUIGlobal?.navigateTo({
      url: '/TUIKit/components/TUIChat/index',
    });
    emits('handleSwitchConversation', conversationID);
  };

  const closeChildren = () => {
    headerRef?.value?.closeChildren();
    conversationListDomRef?.value?.closeChildren();
  };

  const handleClickConv = () => {
    closeChildren();
  };

  onHide(closeChildren);

  const handleTouchStart = (e : any) => {
    touchX.value = e.changedTouches[0].clientX;
    touchY.value = e.changedTouches[0].clientY;
  };

  const handleTouchEnd = (e : any) => {
    const x = e.changedTouches[0].clientX;
    const y = e.changedTouches[0].clientY;
    let turn = '';
    if (x - touchX.value > 50 && Math.abs(y - touchY.value) < 50) {
      // 右滑
      turn = 'right';
    } else if (x - touchX.value < -50 && Math.abs(y - touchY.value) < 50) {
      // 左滑
      turn = 'left';
    }
    if (y - touchY.value > 50 && Math.abs(x - touchX.value) < 50) {
      // 下滑
      turn = 'down';
    } else if (y - touchY.value < -50 && Math.abs(x - touchX.value) < 50) {
      // 上滑
      turn = 'up';
    }
    // 根据方向进行操作
    if (turn === 'down' || turn === 'up') {
      closeChildren();
    }
  };

  const getPassingRef = (ref) => {
    ref.value = conversationListDomRef.value;
  };
  
  
</script>

<style lang="scss" scoped src="./style/index.scss"></style>